/*--------begin change by wangwei----------*/
#include <typedefs.h>
#include <std_ops.h>
#include <task_pub.h>
#include <tasks.h>
#include <time.h>

/*****************************/
/*   GLOBAL VARIABLES        */
/*****************************/
TaskID          Zone_Task_ID;
TaskID          Display_Monitor_ID;
TaskID          Status_Monitor_ID;
TaskID          Keyboard_Cmd_Task_ID;
TaskID          MIO_Cmd_Task_ID;
TaskID          RS232_Cmd_Task_ID;
TaskID          Clock_Table_Task_ID;
TaskID          Sequence_Task_ID;
TaskID          Als_Task_ID;
TaskID          Inj1_Task_ID;
TaskID          Inj2_Task_ID;
TaskID          Tray_Task_ID;
TaskID          Signal_Buffer_Task_ID;
TaskID          Gas_Pass_Task_ID;


TaskID          Run_Task_ID;
TaskID          TUI_Task_ID;
TaskID          UART_Task_ID;


pthread_mutex_t yield_mut = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t yield_cond = PTHREAD_COND_INITIALIZER;

TaskSigs nowSigs = 0;
pthread_mutex_t Data_avail_mut = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t Data_avail_cond = PTHREAD_COND_INITIALIZER;



/*****************************/
/*   FUNCTIONS               */
/*****************************/

TaskName  Task_get_myname(void)
{
  TaskName threadName;
  TaskID threadID = pthread_self();
  if(pthread_equal(threadID,Zone_Task_ID))
  { threadName = ZONE_TASK_NAME;}
else if(pthread_equal(threadID,Display_Monitor_ID))
  { threadName = DISP_MON_NAME;}
else if(pthread_equal(threadID,Status_Monitor_ID))
  { threadName = STAT_MON_NAME;}
else if(pthread_equal(threadID,Keyboard_Cmd_Task_ID))
  { threadName = KBD_CMD_NAME;}
else if(pthread_equal(threadID,MIO_Cmd_Task_ID))
  { threadName = MIO_CMD_NAME;}
else if(pthread_equal(threadID,RS232_Cmd_Task_ID))
  { threadName = RS232_CMD_NAME;}
else if(pthread_equal(threadID,Clock_Table_Task_ID))
  { threadName = CLOCK_TBL_NAME;}
else if(pthread_equal(threadID,Sequence_Task_ID))
  { threadName = SEQUENCE_NAME;}
else if(pthread_equal(threadID,Als_Task_ID))
  { threadName = ALS_NAME;}
else if(pthread_equal(threadID,Inj1_Task_ID))
  { threadName = INJ1_NAME;}
else if(pthread_equal(threadID,Inj2_Task_ID))
  { threadName = INJ2_NAME;}
else if(pthread_equal(threadID,Tray_Task_ID))
  { threadName = TRAY_NAME;}
else if(pthread_equal(threadID,Signal_Buffer_Task_ID))
  { threadName = SIG_BUFFER_NAME;}
else if(pthread_equal(threadID,Gas_Pass_Task_ID))
  { threadName = GAS_PASS_NAME;}

  return threadName;
}
void  Task_yield(void)
{

  pthread_mutex_lock(&yield_mut);
  struct timespec timeout;
  clock_gettime(CLOCK_REALTIME,&timeout);
//  timeout.tv_sec  += 5;
  timeout.tv_nsec += 100000; //100us
  pthread_cond_timedwait(&yield_cond, &yield_mut, &timeout);
  pthread_mutex_unlock(&yield_mut);

}
void  Task_sleep(unsigned int ms)
{

  pthread_mutex_lock(&yield_mut);
  struct timespec timeout;
  clock_gettime(CLOCK_REALTIME,&timeout);
  //  timeout.tv_sec  += 5;
  timeout.tv_nsec += ms*1000000; 
  pthread_cond_timedwait(&yield_cond, &yield_mut, &timeout);
  pthread_mutex_unlock(&yield_mut);

}
TaskID TASK_Current()
{
  return pthread_self();
}
TaskSigs  Task_wait_sig(TaskSigs reqSigs)
{
  TaskSigs temp_Sig = reqSigs & nowSigs;
  if(temp_Sig > 0)  //the sig has exist
    return temp_Sig;

  switch(reqSigs)
  {
  case DATA_AVAIL_SIG:
    pthread_mutex_lock(&Data_avail_mut);
    pthread_cond_wait(&Data_avail_cond, &Data_avail_mut);
    nowSigs = nowSigs & (~reqSigs); //clear DATA_AVAIL_SIG bit automatically for the next wait
    pthread_mutex_unlock(&Data_avail_mut);
    break;
  default:
    break;
  }
}
void  Task_send_sig(TaskSigs sendSigs, TaskID sendID)
{
   nowSigs = nowSigs | sendSigs;

   switch(sendSigs)
   {
   case DATA_AVAIL_SIG:
     pthread_cond_signal(&Data_avail_cond);
     break;
   default:
     break;
   }
}


/*--------end change----------------------*/
